package com.touch.jgloo.owt;
import com.touch.graphics.Renderable;
import java.util.HashMap;
/**
 * The AnimatedSprite is a set of animations that can be set via states. Each animation is added manually through the use of void addAnimation(String, JGLAnimation). If an animation shares the same alias as another animation, the newer one will overwrite the old.
 * @see com/touch/Licence
 * @version 10.2.19
 * @since r471
 */
public class JGLAnimatedSprite implements Renderable, Cloneable
{
	//collection of animations linked to Strings
	private final HashMap<String, JGLAnimation> animationMap = new HashMap<String, JGLAnimation>();
	//currently animating sprite
	private JGLAnimation currentAnimation;
	//the state of the sprite. This is used to determine which animation to use.
	private String state = new String();
	//used for transitions only
	private boolean animating = false;	
	@Override
	public void render() 
	{
		//draw the current animation.
		currentAnimation.render();		
	}
	/**
	 * Adds an animation to the animation map. The added animation then can be accessed by setting the state to the specified alias.
	 * @param alias String representing the animation.
	 * @param animation The animation data as a JGLAnimation.
	 * @since r471
	 */
	public void addAnimation(final String alias, final JGLAnimation animation)
	{
		//add the animation to the animation map.
		this.animationMap.put(alias, animation);
	}
	/**
	 * Sets the state of the animation. The state value is the same as a preset alias for an already existent animation. The animation must exist for the function to operate properly. 
	 * @param state Alias to determine which animation to use.
	 * @since r471
	 */
	public void setState(final String state)
	{
		//check whether the animation is the same as the current animation
		if(this.state.equals(state))
			//if the state is the same, then exit.
			return;		
		//confirm that the animation exists.
		if(this.animationMap.containsKey(state) == false)
			//if it does not exist, then exit.
			return;
		//update the state value with the new one.
		this.state = state;
		//set the current animation to the animation with the specified state (alias).
		this.currentAnimation = animationMap.get(state);
		//reset the animation to the first frame.
		this.currentAnimation.reset();	
		//set the current Animation to animate if the previous was animating.
		this.currentAnimation.setIsAnimating(this.animating);
	}
	/**
	 * Returns the current state of the animation as a String
	 * @return Alias of the current animating frame.
	 * @since r471
	 */
	public String getState()
	{
		//return the animating frame's alias.
		return this.state;
	}
	/**
	 * Returns the specified animation as reference by an alias.
	 * @param state Alias specifying the animation 
	 * @return Animation associated to the specified alias.
	 * @since r471
	 */
	public JGLAnimation getAnimation(final String state)
	{
		//get the animation from the HashMap of animations
		return this.animationMap.get(state);
	}
	/**
	 * Sets whether the frame is animating or not.
	 * @param bAnimating True: the frame will animate; False: the frame will not animate.
	 * @since r471
	 */
	public void setIsAnimation(boolean bAnimating)
	{
		if(this.currentAnimation == null)
			return;
		//set the animating status of the current frame.
		this.currentAnimation.setIsAnimating(bAnimating);
		//define the current animation as animating. Used for transitions.
		this.animating = bAnimating;
	}
	/**
	 * Copies the AnimatedSprite info. Only the animations are cloned. The current animation and animating status are default.
	 * @since r471
	 */
	public JGLAnimatedSprite clone()
	{
		//create a new AnimatedSprite
		final JGLAnimatedSprite clone = new JGLAnimatedSprite();
		//scroll through the list of aliases
		for(String alias: animationMap.keySet())
			//clone the animation and add it.
			clone.addAnimation(alias, animationMap.get(alias).clone());
		//return the new AnimatedSprite.
		return clone;
	}
}
